package org.black.phoneUtility.sms.schedule.app.fragment;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.schedule.database.dataModel.SmsScheduleRecipient;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class SmsScheduleAddRecipientsFromUserInputDialogFragment extends
		DialogFragment {
	public static final String TAG = SmsScheduleAddRecipientsFromUserInputDialogFragment.class
			.getName();
	public static final String ARGUMENTS_SMS_SCHEDULE_ADD_EDIT_RECIPIENT = "addEditRecipient";

	public interface SmsScheduleAddRecipientsFromUserInputDialogInterface {
		public void onChooseSmsScheduleAddRecipientsFromUserInputDialogPositiveClick(
				String phoneNumber, boolean isEditing);

		public void onChooseSmsScheduleAddRecipientsFromUserInputDialogNegativeClick();
	}

	private SmsScheduleAddRecipientsFromUserInputDialogInterface smsReplyAddEditNumberUserInputInterface;
	private SmsScheduleRecipient smsScheduleRecipient;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyAddEditNumberUserInputInterface = (SmsScheduleAddRecipientsFromUserInputDialogInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement SmsReplyAddEditNumberUserInputInterface");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		final Bundle bundle = getArguments() == null ? savedInstanceState
				: getArguments();
		Object recipientObject = bundle
				.getSerializable(ARGUMENTS_SMS_SCHEDULE_ADD_EDIT_RECIPIENT);
		if (recipientObject != null) {
			this.smsScheduleRecipient = (SmsScheduleRecipient) recipientObject;
		}
		View view = getActivity().getLayoutInflater().inflate(
				R.layout.sms_schedule_recipient_add_from_user_input_dialog,
				null);
		EditText addEditNumberUserInputEditText = (EditText) view
				.findViewById(R.id.smsScheduleAddEditNumberUserInputEditText);
		if (this.smsScheduleRecipient != null) {
			addEditNumberUserInputEditText.setText(this.smsScheduleRecipient
					.getPhoneNumber());
		}

		builder.setTitle(R.string.sms_add_edit_from_user_input_title)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								EditText addEditNumberUserInputEditText = (EditText) getDialog()
										.findViewById(
												R.id.smsScheduleAddEditNumberUserInputEditText);
								String phoneNumber = addEditNumberUserInputEditText
										.getText().toString();
								if (smsScheduleRecipient != null) {
									smsScheduleRecipient
											.setPhoneNumber(phoneNumber);
								}
								smsReplyAddEditNumberUserInputInterface
										.onChooseSmsScheduleAddRecipientsFromUserInputDialogPositiveClick(
												addEditNumberUserInputEditText
														.getEditableText()
														.toString(),
												smsScheduleRecipient != null ? true
														: false);

							}
						})
				.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						});

		builder.setView(view);
		return builder.create();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.smsReplyAddEditNumberUserInputInterface = null;
	}

}
